﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Net.Security;

namespace yCloud
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract(ProtectionLevel=ProtectionLevel.None)]
    public interface IYCloudService
    {
        /// <summary>
        /// Adds a folder to the service
        /// </summary>
        /// <param name="name">The name of the folder to add</param>
        /// <param name="parentId">The id of the parent of the new folder</param>
        /// <returns>The id assigned to the new folder or 0 if there is a folder with this name already saved under the same parent, or -1 if the parentId doesn't exist in the service</returns>
        [OperationContract]
        int MakeFolder(string name, int parentId = 0);
        [OperationContract]
        void DeleteFolder(int folderId);
        [OperationContract]
        void RenameFolder(int folderId, string folderName);
        /// <summary>
        /// Uploads a new file to the service
        /// </summary>
        /// <param name="fileData">The content of the file</param>
        /// <param name="fileName">The name of the file</param>
        /// <param name="parentFolder">The id of the folder the file will be in</param>
        /// <returns>The id of the new file, or 0 if a file with this name already exists, or -1 if the parent folder does not exist</returns>
        [OperationContract]
        int UploadFile(byte[] fileData, string fileName, int parentFolder = 0);
        [OperationContract]
        void UpdateFileContent(int fileId, byte[] fileData);
        [OperationContract]
        void UpdateFileInfo(int fileId, string fileName);
        [OperationContract]
        void DeleteFile(int fileId);
        [OperationContract]
        SerializableFolder GetFolder(int folderId);
        [OperationContract]
        SerializableFile GetFile(int fileId);
        [OperationContract]
        SerializableFile GetFileWithoutData(int fileId);
    }

    [DataContract]
    public class SerializableFolder
    {
        private int[] subFolders;
        private int[] files;
        private int id;
        private string name;
        [DataMember]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember]
        public int[] SubFolders
        {
            get { return subFolders; }
            set { subFolders = value; }
        }

        [DataMember]
        public int[] Files
        {
            get { return files; }
            set { files = value; }
        }

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public void Populate(Folder folder)
        {
            this.Name = folder.Name;
            this.SubFolders = folder.Children.Select(c => c.ID).ToArray();
            this.Files = folder.Files.Select(f => f.ID).ToArray();
            this.Id = folder.ID;
        }
    }

    [DataContract]
    public class SerializableFile{
        private int folder;
        private string name;
        private byte[] content;
        private int id;

        [DataMember]
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember]
        public int Folder
        {
            get { return folder; }
            set { folder = value; }
        }

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [DataMember]
        public byte[] Content
        {
            get { return content; }
            set { content = value; }
        }

        public void Populate(File file)
        {
            this.ID = file.ID;
            this.Content = file.Data;
            this.Folder = file.Folder.GetValueOrDefault();
            this.Name = file.Name;
        }
    }
}
